package com.main.action;  
  

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.main.bean.User;
import com.main.service.UserService;
import com.main.util.MD5;
  
  
@Controller   //类似Struts的Action  
@RequestMapping("/login")    
public class LoginAction {  
      
	@Autowired
	private UserService userService;
	
	
	/**
	 * 用户登录
	 * @param session
	 * @param userName
	 * @param password
	 * @return
	 */
	@RequestMapping(value="/login")
    public ModelAndView login(HttpSession session,String userName,String password){
		ModelAndView mv = new ModelAndView();
		if(null!=session.getAttribute("mainUser")){
	        mv.setViewName("index");
		}else{
			User user=new User();
			user.setUser_name(userName);
			userService.getUserByName(user);
			if(null==user.getPassword()||null==user.getCreate_time()){
				 mv.addObject("errorMsg", "用户不存在！");
				 mv.setViewName("login");
			}else if(!"1".equals(user.getState())){
				 mv.addObject("errorMsg", "用户暂不可用！");
				 mv.setViewName("login");
			}else{
				 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
				 MD5 md5 = new MD5();
				 String msg=md5.GetMD5Code(password+sdf.format(user.getCreate_time()));
				 if(msg.equals(user.getPassword())){
					 session.setAttribute("mainUser", user);
					 mv.setViewName("index");
				 }else{
					 mv.addObject("errorMsg", "用户名或密码错误！");
					 mv.setViewName("login");
				 }
			}
		}
        return mv;
    }
	
	
	/**
	 * 用户退出
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/add",method = RequestMethod.POST)
    public ModelAndView add(HttpSession session,@ModelAttribute User user){
		ModelAndView mv = new ModelAndView();
		String message="";
		if(null!=session.getAttribute("mainUser")){
	        mv.setViewName("index");
	        return mv;
		}
		
		if(null==user.getUser_name()||"".equals(user.getUser_name())||user.getUser_name().length()>32){
			message="用户名不符合格式";
			mv.setViewName("login");			
	        mv.addObject("errorMsg",message);		
	        return mv;
		}
		
		 String check = "^[A-Za-z0-9]{1,32}$";  
	     Pattern regex = Pattern.compile(check);  
	     Matcher matcher = regex.matcher(user.getEmail());  
	     boolean isMatched = matcher.matches(); 
		 if(isMatched){
				message="用户名不符合格式";
				mv.setViewName("login");			
		        mv.addObject("errorMsg",message);		
		        return mv;
		  }
		
		if(null==user.getEmail()||"".equals(user.getEmail())||user.getEmail().length()>32){
			message="邮箱不符合格式";
			mv.setViewName("login");			
	        mv.addObject("errorMsg",message);		
	        return mv;
		}
		
		  check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";  
	      regex = Pattern.compile(check);  
	      matcher = regex.matcher(user.getEmail());  
	      isMatched = matcher.matches(); 
		 if(!isMatched){
				message="邮箱不符合格式";
				mv.setViewName("login");			
		        mv.addObject("errorMsg",message);		
		        return mv;
		  }
		int count=userService.getUserCount(user);
		if(count==0){
			user.setCreate_time(new Date());
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			MD5 md5 = new MD5();
			String msg=md5.GetMD5Code(user.getPassword()+sdf.format(user.getCreate_time()));
			user.setPassword(msg);
			count=userService.addUser(user);
			if(count==1){
				message="添加成功，请登录";
			}else{
			  message="添加用户失败";
			}
		}else{
			message="用户已经存在";
		}
		mv.setViewName("login");			
        mv.addObject("errorMsg",message);		
        return mv;
    }
	
	/**
	 * 用户退出
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/out")
    public ModelAndView out(HttpSession session){
		ModelAndView mv = new ModelAndView();
		session.removeAttribute("mainUser");
		mv.setViewName("login");
        return mv;
    }
      
}  